<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends CommonController{

	public $product ;
	public $inventory ;
	public $color ;
	public $size ;
	public $goods_cart ;
	public $orders ; 

	public function _initialize(){
		$this->product = M('product as a');

		$this->inventory = M('pro_inventory');

		$this->color = M('color');

		$this->size = M('size');

		$this->goods_cart = M('goods_cart as a');

		$this->orders = M('orders');
	}

	public function goo_details(){

		if(IS_AJAX){


			$data = I('post.');
			$data['pro_id'] = I('get.pro_id');
			$invent	= $this->inventory->where($data)->field('sum(inventory)inven')->find();
		
			$this->ajaxReturn($invent);
			
		}else{
			

			$pro_id = I('get.pro_id');

			$goods_info = $this->product->field('a.pro_name,b.price,a.pro_id,c.restrict_nub,c.main_img,c.detail_img,c.materials,c.apple_gender,c.article_number,c.detail_intro_img,c.model_id,c.lining')->join('rl_pro_inventory as b on b.pro_id = a.pro_id')->join('rl_pro_attr as c on c.pro_id = a.pro_id')->where(array('a.pro_id'=>$pro_id))->find();

			// dump($goods_info);die;
			$goods_inventory = $this->inventory->where(array('pro_id'=>$pro_id))->select();

			$th_color = array_unique(array_column($goods_inventory,'color_id'));
			foreach($th_color as $val){
				$colors[] = $this->color->where(array('color_id'=>$val))->find();
			}

			$th_size = array_unique(array_column($goods_inventory,'size_id'));
			foreach($th_size as $val){
				$sizes[] = $this->size->where(array('size_id'=>$val))->find();
			}
			// row_number() over (partition by [手机号] order by [店铺])
			$las = $this->product->field('min(price)price,a.pro_id,pro_name')->join('rl_pro_inventory as b on b.pro_id = a.pro_id')->join('rl_pro_attr as c on c.pro_id = a.pro_id')->group('pro_name')->limit(0,3)->select();

			

			$th_inventory = array_column($goods_inventory,'inventory');
			$sum_inven = array_sum($th_inventory);



			$this->assign(array(
				'goods_info'	=>	$goods_info,
				'colors'		=>	$colors,
				'sizes'			=>	$sizes,
				'sum_inven'		=>	$sum_inven,
				'las'			=>	$las,
			));

			$this -> display();
		}

	}

	public function collect(){
		$info = ['code' => 0 , 'status' => false , 'info' => ''];
		if(IS_POST){

			if(!empty(session('user_id'))){
				$collect = M('collect');


				$data['pro_id'] = I('post.pro_id');
				$data['user_id'] = session('user_id');
				
				$exist = $collect->where($data)->find();
				if(!$exist){
					$data['addtime'] = time();
					$colet = $collect->add($data);
					if(!$colet){
						$info['info'] = '收藏失败';
						$this->ajaxReturn($info);
					}else{
						$info['code'] = 1;
						$info['status'] =true;
						$info['info'] = '收藏成功';
						$this->ajaxReturn($info);
					}
				}else{
					$info['info'] = '已收藏的商品';
					$this->ajaxReturn($info);
				}
				
			}else{
				// $this->redirect('Home/Login/login');
				$info['code'] = 2;
				$info['info'] = '未登录';
				$this->ajaxReturn($info);
			}
		}
	}


	//添加购物车
	public function add_cart(){
		if(IS_POST){
			if(!empty(session('user_id'))){
				$info = ['code' => 0 , 'status' => false , 'info' => ''];
				$data = I('post.');
				$data['user_id'] = session('user_id');

				if(!empty($data['color_id']) && !empty($data['size_id'])){;
					// $exist = 
					$where = $data;
					$where['status'] = 0;
					unset($where['number']);

					$exist = $this->goods_cart->where($where)->find();
					unset($where['status']);
					$invent = $this->inventory->where($where)->find();
					if(!$exist && $invent['inventory']){
						$data['addtime'] = time();
						$goods_cart = M('goods_cart');
						$add_cart = $goods_cart->add($data);
						if(!$add_cart){
							$info['info'] = '添加失败';
							$this->ajaxReturn($info);
						}else{
							$info['info'] = '添加成功';
							$info['code'] = 1;
							$info['status'] = true;
							$this->ajaxReturn($info);
						}
					}elseif($exist && $invent['inventory']){
						$data['addtime'] = time();
						$data['number'] = $exist['number']+$data['number'];
						$upd_cart = $this->goods_cart->where($where)->save($data);
						if(!$upd_cart){
							$info['info'] = '添加失败';
							$this->ajaxReturn($info);
						}else{
							$info['info'] = '添加成功';
							$info['code'] = 1;
							$info['status'] = true;
							$this->ajaxReturn($info);
						}
					}elseif(!$invent['inventory']){
						$info['info'] = '库存不足';
						$this->ajaxReturn($info);
					}
				
				}else{
					$info['info'] = '请选择颜色和尺寸';
					$this->ajaxReturn($info);
				}
			}else{
				$info['code'] = 2;
				$info['info'] = '未登录';
				$this->ajaxReturn($info);
			}
			
		}
	}


	public function shop_cart(){

		if(!empty(session('user_id'))){
			if(IS_POST){

				
				if(I('post.status') == 1){
					$data = I('post.');
					$this->goods_cart->save($data);
				}elseif(I('post.status') == 2){
					$goods_cart = M('goods_cart');
					$info = ['code' => 0 , 'status' => false , 'info' => ''];
					$cart_del = $goods_cart->where(array('cart_id'=>I('post.cart_id')))->delete();
					if($cart_del){
						$info['code'] = 1;
						$info['status'] = true;
						$info['info'] = '删除成功';
						$this->ajaxReturn($info);
					}else{
						$info['info'] = '删除失败';
						$this->ajaxReturn($info);
					}
				}elseif(I('post.status') == 3){
					$cart_ids = I('post.arr');
					session('cart_ids',NULL);
					session('cart_ids',$cart_ids);
					exit;
				
				}
				

			}else{
				$field = 'a.pro_id,a.size_id,a.number,a.cart_id,b.pro_name,c.color_name,d.size_scope,f.catename,g.price';
				$goo_cart = $this->goods_cart->field($field)->where(array('user_id' => session('user_id'),'statu'=>'0'))->join('rl_product as b on b.pro_id=a.pro_id')->join('rl_color as c on c.color_id=a.color_id')->join('rl_size as d on d.size_id=a.size_id')->join('rl_pro_cate as e on e.pro_id=a.pro_id')->join('rl_category as f on f.cate_id=e.cate_id')->join('rl_pro_inventory as g on g.size_id=a.size_id and g.color_id=a.color_id and g.pro_id=a.pro_id')->select();

				
				// dump($goo_cart);die;


				$this->assign(array(
					'goo_cart'		=>	$goo_cart,
				));
				$this->display();

			}
		}else{
			$this->redirect('Login/login');
		}
	}


	public function buy(){
		if(!empty(session('user_id'))){
			if(IS_POST){
				$info = ['code' => 0 ,'status' => false, 'info' => ''];
				$goods_cart = M('goods_cart');
				$data = I('post.');
				if($data['place'] =='detail'){
					if(!isset($data['color_id'])){
						$info['info'] = '请选择颜色';
						$this->ajaxReturn($info);
					}elseif(!isset($data['size_id'])){
						$info['info'] = '请选择尺寸';
						$this->ajaxReturn($info);
					}elseif($data['number']<=0){
						$info['info'] = '数量至少为1';
						$this->ajaxReturn($info);
					}else{
						$data['user_id'] = session('user_id');
						$data['statu'] = 1;
						$data['addtime'] = time();
						$cart_id = $goods_cart->add($data);
						if(!$cart_id){
							$info['info'] = '购买失败';
							$this->ajaxReturn($info);
						}else{
							$info['code'] = 1;
							$info['status'] = true;
							session('cart_ids',NULL);
							session('order_id',NULL);
							session('cart_ids',$cart_id);
							$this->ajaxReturn($info);
						}
					}
				}elseif($data['place'] == 'order'){
					$order_id = I('post.order_id');
					$order_info = $this->orders->field('cart_ids')->where(array('order_id'=>$order_id))->find();
					if(!$order_info){
						$info['info'] = '跳转失败';
						$this->ajaxReturn($info);
					}else{
						$info['code'] = 1;
						$info['status'] = true;
						session('cart_ids',NULL);
						session('order_id',NULL);
						session('cart_ids',$order_info['cart_ids']);
						session('order_id',$order_id);
						$this->ajaxReturn($info);
					}
				}
			}
		}else{
			$info['info'] = '请登录';
			$this->ajaxReturn($info);
		}
	}

			
}


	



?>